package at.fhj.itm11.forum.service.impl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.UploadedFile;

import at.fhj.itm11.forum.data.PostDAO;
import at.fhj.itm11.forum.data.TopicDAO;
import at.fhj.itm11.forum.data.UserDAO;
import at.fhj.itm11.forum.model.Post;
import at.fhj.itm11.forum.service.PostService;
import at.fhj.itm11.forum.service.RatingService;
import at.fhj.itm11.forum.service.converter.DTOConverter;
import at.fhj.itm11.forum.service.converter.PostDTOConverter;
import at.fhj.itm11.forum.service.dtos.PostDTO;
import at.fhj.itm11.forum.service.dtos.TopicDTO;
import at.fhj.itm11.forum.service.dtos.UserDTO;

@Stateless
@Named
public class PostServiceImpl implements PostService {
	private DTOConverter<PostDTO, Post> postDtoConverter = new PostDTOConverter();

	@Inject
	@Named("postDAOImpl")
	private PostDAO postDao;

	@Inject
	@Named("ratingServiceImpl")
	private RatingService ratingService;

	@Inject
	@Named("userDAOImpl")
	private UserDAO userDao;

	@Inject
	@Named("topicDAOImpl")
	private TopicDAO topicDao;

	@Override
	public List<PostDTO> getPostsByTopicId(long topicId) {
		List<Post> posts = postDao.findPostsByTopic(topicId);

		List<PostDTO> postDtos = new LinkedList<PostDTO>();

		for (Post p : posts) {
			int[] ratingStatistic = ratingService
					.getRatingStatistics(p.getId());

			PostDTO postDto = postDtoConverter.convertToDto(p);
			postDto.setPercentageOfPositiveRatings(ratingStatistic[0]);
			postDto.setPercentageOfNegativeRatings(ratingStatistic[1]);

			postDtos.add(postDto);
		}

		return postDtos;
	}

	@Override
	public long addPost(String title, String text, UploadedFile image,
			UploadedFile video, TopicDTO topicDto, UserDTO user) {

		if (title == null || "".equals(title)) {
			throw new IllegalArgumentException(EXCEPTION_TITLE_EMPTY);
		}

		if (text == null || "".equals(text)) {
			throw new IllegalArgumentException(EXCEPTION_TEXT_EMPTY);
		}

		Post newPost = new Post();

		if (!"".equals(image.getFileName())) {
			if (image.getContentType().startsWith("image/")) {
				byte[] byteFile = new byte[image.getContents().length];
				System.arraycopy(image.getContents(), 0, byteFile, 0,
						image.getContents().length);
				newPost.setImage(byteFile);
			} else {
				throw new IllegalArgumentException(EXCEPTION_ONLY_IMAGES);
			}
		}

		if (!"".equals(video.getFileName())) {
			if (video.getContentType().startsWith("video/")) {
				byte[] byteFile = new byte[video.getContents().length];
				System.arraycopy(video.getContents(), 0, byteFile, 0,
						video.getContents().length);
				newPost.setVideo(byteFile);
			} else {
				throw new IllegalArgumentException(EXCEPTION_ONLY_VIDEOS);
			}
		}

		newPost.setTitle(title);
		newPost.setText(text);
		newPost.setDate(new Date());
		newPost.setTopic(topicDao.findById(topicDto.getId()));
		newPost.setUser(userDao.findById(user.getId()));

		postDao.persist(newPost);

		return newPost.getTopic().getId();
	}

	@Override
	public PostDTO findById(long id) {
		return postDtoConverter.convertToDto(postDao.findById(id));
	}
}
